package com.jsgt.greekfire.ui.gfc

import android.app.Dialog
import android.os.Bundle
import android.text.TextUtils
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.fhx.core.base.BaseFragment
import com.fhx.core.utils.ToastUtils
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.MyOrderBean
import com.jsgt.greekfire.bean.MyUserAdBean
import com.jsgt.greekfire.common.util.XLinearLayoutManager
import com.jsgt.greekfire.factory.CoinGFCModelFactory
import com.jsgt.greekfire.ui.gfc.adapter.CommissionAdapter
import com.jsgt.greekfire.ui.gfc.adapter.UserAdAdapter
import com.jsgt.greekfire.viewmodel.CoinCurrencyViewModel
import com.scwang.smartrefresh.header.MaterialHeader
import kotlinx.android.synthetic.main.fragment_commission.*

private const val ARG_PARAM1 = "param1"

class CommissionFragment : BaseFragment() {
    private var param1: String? = null
    var carList: ArrayList<MyOrderBean.ListBean>? = null

    var carList1: ArrayList<MyUserAdBean.ListBean>? = null

    var ivDialogClosed: ImageView? = null
    var tRvecall: TextView? = null
    var cancel: TextView? = null
    private var page = 1
    private var page1 = 1
    private var dealState = 1
    private val coinCurrencyViewModel: CoinCurrencyViewModel by lazy {
        ViewModelProvider(
            this,
            CoinGFCModelFactory()
        ).get(CoinCurrencyViewModel::class.java)
    }

    private val commissionAdapter by lazy {

        CommissionAdapter(context!!, R.layout.item_commission_list)
    }
    private val userAdAdapter by lazy {

        UserAdAdapter(context!!, R.layout.item_commission_list)
    }

    companion object {
        @JvmStatic
        fun newInstance(param1: String) =
            CommissionFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                }
            }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
        }
    }


    override fun getLayoutId(): Int = R.layout.fragment_commission
    override fun main() {
        carList1 = ArrayList()

        refreshCommission.autoRefresh()
        refreshCommission.setRefreshHeader(MaterialHeader(context));
        refreshCommission.setOnRefreshListener {
            if (!TextUtils.isEmpty(param1)) {
                if (param1.equals("我的订单")) {
                    page = 1;
                    coinCurrencyViewModel.getUserOrderData(page)
                } else {
                    page1 = 1
                    coinCurrencyViewModel.getUserAd(page1, dealState)
                }
            }
        }
        refreshCommission.setOnLoadMoreListener {
            if (!TextUtils.isEmpty(param1)) {
                if (param1.equals("我的订单")) {
                    coinCurrencyViewModel.getUserOrderData(++page)
                } else {
                    coinCurrencyViewModel.getUserAd(++page1, dealState)
                }
            }
        }
        rvCommission.layoutManager = XLinearLayoutManager(context)
        if (!TextUtils.isEmpty(param1)) {
            if (param1.equals("我的订单")) {
                rvCommission.adapter = commissionAdapter
                commissionAdapter.setOnItemClickListener { adapter, view, position ->
//            showCommissionDialog(commissionAdapter.data.get(position));
                }
            } else {
                rvCommission.adapter = userAdAdapter
                userAdAdapter.setOnItemClickListener { adapter, view, position ->
                    showCommissionDialog(userAdAdapter.data.get(position).guid.toString());
                }
            }
        }
        coinCurrencyViewModel.userOrderData.observe(this, Observer {
            carList = arrayListOf()
            if (page == 1) {
                carList!!.clear();
            }
            if (it.result?.list!!.size <= 0) {
                if (refreshCommission != null) {
                    refreshCommission.finishRefresh();
                    refreshCommission.finishLoadMore();//不传时间则立即停止刷新    传入false表示加载失败
                    refreshCommission.finishLoadMoreWithNoMoreData();//没有加载更多
                }
            } else {
                carList!!.addAll(it.result?.list!!)

            }
            if (carList!!.size <= 0) {
                commissionAdapter.setEmptyView(R.layout.item_empty)
            }
            commissionAdapter?.setNewData(carList!!)
            if (refreshCommission != null) {
                refreshCommission.finishRefresh();
                refreshCommission.finishLoadMore();//不传时间则立即停止刷新    传入false表示加载失败
            }

        })
        coinCurrencyViewModel.userAdData.observe(this, Observer {
            if (page1 == 1) {
                carList1!!.clear();
            }
            if (it.result?.list!!.size <= 0) {
                if (refreshCommission != null) {
                    refreshCommission.finishRefresh();
                    refreshCommission.finishLoadMore();//不传时间则立即停止刷新    传入false表示加载失败
                    refreshCommission.finishLoadMoreWithNoMoreData();//没有加载更多
                }
            } else {
                carList1!!.addAll(it.result?.list!!)

            }
            if (carList1!!.size <= 0)
                userAdAdapter!!.setEmptyView(R.layout.item_empty)

//            userAdAdapter!!.replaceData(carList1!!)
            if (refreshCommission != null) {
                refreshCommission.finishRefresh();
                refreshCommission.finishLoadMore();//不传时间则立即停止刷新    传入false表示加载失败
            }

        })
        coinCurrencyViewModel.adDownData.observe(this, Observer {
            showProgress(false)
            if (refreshCommission != null) {
                refreshCommission.autoRefresh()
            }
        })
        coinCurrencyViewModel.errorLiveData.observe(this, Observer {
            showProgress(false)
            if (refreshCommission != null) {
                refreshCommission.finishRefresh();
                refreshCommission.finishLoadMore();//不传时间则立即停止刷新    传入false表示加载失败
            }
        })
    }

    /**
     * 选择展示设置
     */
    private fun showCommissionDialog(guid: String) {
        val button1Dialog: Dialog = Dialog(context!!, R.style.DialogTheme)
        val view: View = LayoutInflater.from(context!!).inflate(R.layout.dialog_commission, null)
        button1Dialog.setContentView(view)
        var layoutParams: ViewGroup.LayoutParams = view.layoutParams
        layoutParams.width = resources.displayMetrics.widthPixels
        view.layoutParams = layoutParams
        button1Dialog.window!!.setGravity(Gravity.BOTTOM)
        button1Dialog.window!!.setWindowAnimations(R.style.main_menu_animStyle)
        button1Dialog.show()

        ivDialogClosed = view.findViewById(R.id.ivDialogClosed)
        tRvecall = view.findViewById(R.id.tRvecall)
        cancel = view.findViewById(R.id.cancel)

        ivDialogClosed?.setOnClickListener { v ->
            button1Dialog.dismiss()
        }
        tRvecall?.setOnClickListener { v ->
            showProgress(true)
            if (!TextUtils.isEmpty(guid)) {
                coinCurrencyViewModel.cancellationOrders(guid)
            }
            button1Dialog.dismiss()
        }
        cancel?.setOnClickListener { v ->
            button1Dialog.dismiss()
        }


    }

}